<?php

namespace App\Http\Controllers\Api;

use App\Models\Notice;
use App\Models\Renting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RentingController extends Controller
{
    //租客资料
    public function renting(Request $request){
        //当前用户id
        $id=$request->get('id');
        $data=$request->only([
            'truename',
            'phone',
            'email',
            'age',
            'card',
            'card_img'
        ]);
        Renting::where('id',$id)->update($data);
        return ['code'=>0,'msg'=>'更新成功','data'=>$data];
    }
    //以id返回对应的数据  因为使用redis缓存 手动查询数据 根据缓存判断是否查询数据库
    public function show(int $renting){
        $key='renting_'.$renting;
        //判断缓存key是否存在，存在返回缓存数据，过期或不存在，执行匿名函数，重新缓存数据并返回
        $data=cache()->remember($key,10,function ()use ($renting){
            return Renting::find($renting);
        });
        return ['code'=>0,'msg'=>'成功','data'=>$data];
    }
    //文件上传
    public function upfile(Request $request){
        $pic='uploads/noimg.jpg';
        //上传节点名称
        $upNode=$request->get('node','article');
        if($request->hasFile('file')){
            $file=$request->file('file');
            $filename=$file->store($upNode,'upfile');
            $pic='/uploads/'.$filename;
        }
        return ['pic'=>$pic];
    }
    //租客对应的通知信息
    public function notice(Request $request){
        //获取租客id
        $renting_id=$request->get('renting_id');
        $data=Notice::with('owner')->where('renting_id',$renting_id)->paginate(config('app.pagesize'));
        return ['code'=>0,'msg'=>'成功','data'=>$data];
    }
}
